<?php
namespace app\admin\controller;

use think\facade\View;

use app\admin\model\SysLogin;
use app\admin\model\TopicContent;
use app\admin\model\SysUser;

class Index extends Base
{
    public function index()
    {
        $id = $this->uid;
        $loginNoteModel = new SysLogin();
        $dataLogin = $loginNoteModel->where('userid', $id)->order('id desc')->limit(10)->select();
        $loginCounts = $loginNoteModel->where('userid', $id)->count();
        
        return View::fetch('index', [
            'loginCounts' => $loginCounts,
            'dataLogin' => $dataLogin,
            'topics' => TopicContent::count('id'),
            'users' => SysUser::count('id')
        ]);
    }

    public function usedspace()
    {
        if ($this->app->request->isAjax()) {
            $dirname = $this->app->getRootPath();
            $dirsize = get_dir_size($dirname);
        
            define('BKM', 1024);
    
            if ($dirsize>BKM) {
                $dirsize =  round($dirsize/BKM, 2);
    
                if ($dirsize>BKM) {
                    $dirsize = round($dirsize/BKM, 2);
                
                    $dirsize .="M";
                } else {
                    $dirsize .="K";
                }
            } else {
                $dirsize .="B";
            }
    
            return ['code'=>1, 'size'=>$dirsize];
        }
    }

    public function clearcache()
    {
        if ($this->app->request->isAjax()) {
            $temp_dir =  $this->app->getRuntimePath() . 'temp/';
            $cache_dir =  $this->app->getRuntimePath() . 'cache/';
            try {
                deldir($temp_dir);
                deldir($cache_dir);
            } catch (\Exception $e) {
                return ['code'=>0, 'msg'=>$e->getMessage()];
            }
            return ['code'=>1, 'msg'=>'清除成功'];
        }
    }
}
